




let BaseController = require('./BaseController')
let fs = require('fs')
let path = require('path')
class Controller extends BaseController {
    constructor() {
        super();
        this.baseSrc = `d:/数据指标分类/${new Date().format('yyyyMMdd')}/待入库/06日龙虎榜/个股龙虎榜/`
        this.encode = 'GBK'
        this.params = {
            pageIndex: 1,
            pageTotal: ''
        }


        this.data = []
    }
    /**
     * 获取url
     */
    getUrl(date) {
        //http://data.eastmoney.com/stock/stockstatistic.html
        return `http://data.eastmoney.com/DataCenter_V3/stock2016/StockStatistic/pagesize=50,page=${this.params.pageIndex},sortRule=-1,sortType=,startDate=${this.params.date},endDate=${this.params.date},gpfw=0,js=var%20data_tab_3.html?rt=25563002`
    }

    /**
     * 获取历史数据
     */
    async getHistory() {
        console.log('40个股龙虎榜Controller')
        //--------------------
        try {
            if (new Date() >= new Date(this.params.date)) {
                if (this.params.pageTotal === '' || this.params.pageIndex <= this.params.pageTotal) {

                    let result = await this.getData('', this.isCache)
                    result = this.axiosDataFormat(result.toString());
                    let pageDate = result.data;
                    this.params.pageTotal = result.pages;
                    console.log(JSON.stringify(this.params))
                    this.data = [...this.data, ...pageDate]
                    this.params.pageIndex++;
                } else {
                    this.writeCsv(this.data);
                    this.data = [];
                    this.params.pageTotal = ''
                    this.params.pageIndex = 1;
                    this.params.date = new Date(this.addDate(this.params.date, 1)).format('yyyy-MM-dd')
                }
                await this.getHistory()
            }
        } catch (err) {
            await this.getHistory()
        }
    }
    /**
     * 数据格式化
     */
    axiosDataFormat(data) {
        let result = null
        try {
            result = JSON.parse(data.replace(/^var\s\w+?\s*\=/, '').trim())
        } catch (err) {
            try {
                result = eval('(' + data.replace(/^var\s\w+?\s*\=/, '').trim() + ')')
            } catch (err) {
                if (data.indexOf('502 Bad Gateway') > -1) {
                    return []
                } else {
                    console.log(err)
                }

            }
        }
        return result
    }

    /**
     * 写csv
     */
    writeCsv(arr) {
        if (!fs.existsSync(this.baseSrc)) {
            this.mkdirs(this.baseSrc)
        }
        if (arr && arr.length) {
            fs.writeFileSync(path.join(this.baseSrc, `个股龙虎榜_${this.params.date.replace(/-/g, '')}_${new Date().format('yyyyMMdd')}.csv`), this.convertToCSV(arr, 'title'), 'UTF-8')
        }
    }

    async main(date = '2015-1-1', isCache = true) {
        this.params.date = new Date(date).format('yyyy-MM-dd');
        this.isCache = isCache;
        await this.getHistory()
    }
}

let controller = new Controller()
// controller.main('2018-6-6')
// module.exports = controller
module.exports = controller